package com.xiaobaibai.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaobaibai.entity.DistributionPay;
import com.xiaobaibai.entity.DistributionRecording;
import com.xiaobaibai.entity.DistributionUser;
import com.xiaobaibai.response.ResponseFormat;
import com.xiaobaibai.service.IDistributionPayService;
import com.xiaobaibai.service.IDistributionRecordingService;
import com.xiaobaibai.service.IDistributionUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "分销管理接口")
@RequestMapping("/distribution")
@CrossOrigin
public class DistributionController {

    @Autowired
    private IDistributionUserService distributionUserService;

    @Autowired
    private IDistributionRecordingService distributionRecordingService;

    @Autowired
    private IDistributionPayService distributionPayService;

    /**
     * 1.查看分销表列表
     * 2.个人分销记录列表
     * 3.提现申请列表
     * 4.同意申请
     */

    @GetMapping("/list")
    @ApiOperation("查看分销列表")
    public ResponseFormat list(@RequestParam(value = "userId",required = false)Integer userId,
                               @RequestParam(value = "code",required = false)String code,
                               @RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                               @RequestParam(value = "pageSize",required = false,defaultValue = "20")Integer pageSize){
        /**
         * 根据修改时间排序
         * 一直根据userId查询
         */
        QueryWrapper<DistributionUser> q1=new QueryWrapper<>();
        if(userId!=null)
            q1.eq("user_id",userId);
        if(code!=null)
            q1.eq("invitation_code",code);
        q1.orderByDesc("update_time");
        IPage<DistributionUser> page=new Page<>(pageNum,pageSize);
        IPage<DistributionUser> result = distributionUserService.page(page, q1);
        return ResponseFormat.ok("data",result);
    }

    @GetMapping("/recording")
    @ApiOperation("查看个人分销记录")
    public ResponseFormat lookRecording(@RequestParam(value = "id")Integer id,
                                        @RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                                        @RequestParam(value = "pageSize",required = false,defaultValue = "20")Integer pageSize){
        QueryWrapper<DistributionRecording> q1=new QueryWrapper<>();
        q1.eq("distribution_id",id);
        q1.orderByDesc("id");
        IPage<DistributionRecording> page=new Page<>(pageNum,pageSize);
        IPage<DistributionRecording> result = distributionRecordingService.page(page, q1);
        return ResponseFormat.ok("data",result);
    }

    @GetMapping("/appPayList")
    @ApiOperation("查看提现申请列表")
    public ResponseFormat appPayList(@RequestParam(value = "userId",required = false)Integer userId,
                                     @RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                                     @RequestParam(value = "pageSize",required = false,defaultValue = "20")Integer pageSize){
        QueryWrapper<DistributionPay> q1=new QueryWrapper<>();
        if(userId!=null)
            q1.eq("user_id",userId);
        q1.orderByDesc("id");
        IPage<DistributionPay> page=new Page<>(pageNum,pageSize);
        IPage<DistributionPay> result = distributionPayService.page(page, q1);
        return ResponseFormat.ok("data",result);
    }

    @GetMapping("/agreeApp")
    @ApiOperation("同意申请")
    public ResponseFormat agreeApp(@RequestParam(value = "id") Integer id){
        UpdateWrapper<DistributionPay> u1=new UpdateWrapper<>();
        u1.set("status",true);
        u1.eq("id",id);
        distributionPayService.update(u1);
        return ResponseFormat.ok("内部逻辑已同意");
    }

}
